package com.bootdo.collection.service.impl;

import com.bootdo.collection.dao.CollectionMenuDao;
import com.bootdo.collection.domain.CollectionMenuDO;
import com.bootdo.collection.service.CollectionMenuService;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.system.dao.MenuDao;
import com.bootdo.system.domain.MenuDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.ref.PhantomReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CollectionMenuServiceImpl implements CollectionMenuService{
    @Autowired
    private CollectionMenuDao collectionMenuMapper;
    @Override
    public List<Tree<CollectionMenuDO>> navListMenuTree(Map<String,Object> params) {
        List<CollectionMenuDO> collectionMenuDOS = collectionMenuMapper.navListMenu(params);
        List<Tree<CollectionMenuDO>> trees = new ArrayList<Tree<CollectionMenuDO>>();
        for (CollectionMenuDO collectionMenuDO : collectionMenuDOS) {
            Tree<CollectionMenuDO> tree = new Tree<CollectionMenuDO>();
            tree.setId(collectionMenuDO.getMenuId().toString());
            tree.setParentId(collectionMenuDO.getParentId().toString());
            tree.setText(collectionMenuDO.getName());
            Map<String, Object> attributes = new HashMap<>(16);
            attributes.put("url", collectionMenuDO.getUrl());
            attributes.put("icon", collectionMenuDO.getIcon());
            tree.setAttributes(attributes);
            trees.add(tree);
        }
        // 默认顶级菜单为０，根据数据库实际情况调整
        List<Tree<CollectionMenuDO>> list = BuildTree.buildList(trees,"0");
        return list;
    }
}
